<script setup lang="ts">
import { findTopCategory } from '@/api/category'
import { findBanner } from '@/api/home'
import { useCategoryStore } from '@/stores'
import type { FindTopCategoryChildType } from '@/types/category'
import type { FindBannerType } from '@/types/home'
import { computed, onMounted, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import GoodsItem from './components/goods-item.vue'

// 定义仓库
const store = useCategoryStore()
// 获取路由信息
const routr = useRoute()

// 定义轮播图数据
const sliders = ref<FindBannerType[]>([])
// 请求轮播图数据
onMounted(async () => {
  const res = await findBanner()
  sliders.value = res.result
})
// 获取顶级分类
const topCategory = computed(() => {
  return store.categoryDataList.find((item) => {
    return item.id === routr.params.id
  })
})

// 推荐商品数组
const subList = ref<FindTopCategoryChildType[]>([])
// 定义获取数据函数
const getSubList = async () => {
  const id = routr.params.id as string
  const res = await findTopCategory(id)
  subList.value = res.result.children
}

// 路由规则不变，组件不会重新加载，所以要监听路径变化来请求数据
watch(
  () => routr.params.id,
  (newVal) => {
    // 判断只有是顶级分类才调接口，二级分类不调用
    if (newVal && `/category/${newVal}` === routr.path) getSubList()
  },
  {
    immediate: true
  }
)
</script>

<template>
  <div class="top-category">
    <div class="container">
      <!-- 多个动画同时进行会卡顿，切换动画模式，改成先出后进 -->
      <!-- 加key才有动画切换效果 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <Transition name="fade-right" mode="out-in">
          <XtxBreadItem :key="topCategory?.id">
            {{ topCategory?.name }}
          </XtxBreadItem>
        </Transition>
      </XtxBread>
      <!-- 轮播图 -->
      <XtxCarousel :sliders="sliders" style="height: 500px" />
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in topCategory?.children" :key="item.id">
            <a href="javascript:;">
              <img v-Lazy="item.picture" />
              <p>{{ item.name }}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="item in subList" :key="item.id">
        <div class="head">
          <h3>- {{ item.name }} -</h3>
          <p class="tag">{{ item.name }}</p>
          <XtxMore />
        </div>
        <div class="body">
          <GoodsItem v-for="g in item.goods" :key="g.id" :goods="g" />
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }
}
</style>
